package com.gsafety.wellsfort.service.organization.impl;

import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.enums.StatusEnum;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.MapUtils;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.dao.organization.ClassDAO;
import com.gsafety.wellsfort.dao.organization.SchoolDAO;
import com.gsafety.wellsfort.dao.organization.StudentDAO;
import com.gsafety.wellsfort.dao.organization.StudentLoginRelDAO;
import com.gsafety.wellsfort.dao.security.UserLoginDAO;
import com.gsafety.wellsfort.domain.model.organization.Class;
import com.gsafety.wellsfort.domain.model.organization.School;
import com.gsafety.wellsfort.domain.model.organization.Student;
import com.gsafety.wellsfort.domain.model.organization.StudentLoginRel;
import com.gsafety.wellsfort.domain.model.security.UserLogin;
import com.gsafety.wellsfort.domain.vo.StudentQueryVO;
import com.gsafety.wellsfort.domain.vo.StudentResultVO;
import com.gsafety.wellsfort.domain.vo.StudentVO;
import com.gsafety.wellsfort.service.course.CourseService;
import com.gsafety.wellsfort.service.organization.IStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentDAO studentDAO;

    @Autowired
    private SchoolDAO schoolDAO;

    @Autowired
    private ClassDAO classDAO;

    @Autowired
    private UserLoginDAO userLoginDAO;

    @Autowired
    private StudentLoginRelDAO studentLoginRelDAO;

    @Autowired
    private CourseService courseService;


    @Override
    public ApiResponse add(StudentVO studentVO) {
        Student student = new Student();
        Date date = new Date();
        BeanUtils.copyProperties(studentVO, student);
        student.setCreator(LoginContextUtil.getUserName());
        student.setOperator(LoginContextUtil.getUserName());
        student.setCreated(date);
        student.setExt1("1");
        student.setModified(date);
        if (studentVO.getStatus() != null) {
            student.setStatus(studentVO.getStatus().byteValue());
        } else {
            student.setStatus(StatusEnum.ENABLE.getValue().byteValue());
        }
        int inserted = studentDAO.insertSelective(student);
        if (inserted > 0) {
            courseService.publishHistoryCourseToStudent(Arrays.asList(student), false, null);
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    @Override
    public Integer addStudent(Student student) {
        Date date = new Date();
        student.setCreated(date);
        student.setModified(date);
        if (student.getStatus() == null) {
            student.setStatus(StatusEnum.ENABLE.getValue().byteValue());
        }
        int inserted = studentDAO.insertSelective(student);
        courseService.publishHistoryCourseToStudent(Arrays.asList(student), false, null);
        return inserted;
    }

    @Override
    public ApiResponse update(StudentVO studentVO) {
        Student student = new Student();
        Date date = new Date();
        BeanUtils.copyProperties(studentVO, student);
        student.setOperator(LoginContextUtil.getUserName());
        student.setModified(date);
        if (studentVO.getStatus() != null) {
            student.setStatus(studentVO.getStatus().byteValue());
        } else {
            student.setStatus(StatusEnum.ENABLE.getValue().byteValue());
        }
        int inserted = studentDAO.updateByPrimaryKeySelective(student);
        if (inserted > 0) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    @Override
    public ApiResponse delete(Integer studentId) {
        int deleted = studentDAO.deleteByPrimaryKey(studentId);
        if (deleted > 0) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    @Override
    public Student get(Integer id) {
        return studentDAO.selectByPrimaryKey(id);
    }

    @Override
    public Student getByLoginId(Integer loginId) {
        return studentDAO.getByLoginId(loginId);
    }

    @Override
    public Student getAllByLoginId(Integer loginId) {
        return studentDAO.getAllByLoginId(loginId);
    }

    @Override
    public Integer unBindAccount(Integer loginId) {
        return studentLoginRelDAO.deleteByLoginId(loginId);
    }

    @Override
    public Integer bindAccount(Integer studentId, Integer loginId) {
        Date date = new Date();
        StudentLoginRel studentLoginRel = new StudentLoginRel();
        studentLoginRel.setLoginId(loginId);
        studentLoginRel.setStudentId(studentId);
        studentLoginRel.setCreated(date);
        studentLoginRel.setCreated(date);
        return studentLoginRelDAO.insertSelective(studentLoginRel);
    }

    @Override
    public ApiResponse batchAddStudent(List<StudentVO> list, Integer schoolId, Integer classId) {
        if (list != null && list.size() > 0) {
            for (StudentVO student : list) {
                if (StringUtils.isEmpty(student.getName())) {
                    return ApiResponse.error(ResultCode.ERROR, "学生姓名不能为空!");
                }
                if (StringUtils.isEmpty(student.getParentName())) {
                    return ApiResponse.error(ResultCode.ERROR, "家长姓名不能为空!");
                }
                if (StringUtils.isEmpty(student.getParentPhone())) {
                    return ApiResponse.error(ResultCode.ERROR, "家长联系方式不能为空!");
                }
                if (student.getStatus() == null || (student.getStatus().intValue() != 0 && student.getStatus().intValue() != 1)) {
                    return ApiResponse.error(ResultCode.ERROR, "状态输入不合法!");
                }
                student.setSchoolId(schoolId);
                student.setClassId(classId);
                this.add(student);
            }
        }
        return ApiResponse.success();
    }

    @Override
    public Integer count() {
        return studentDAO.count();
    }

    @Override
    public Integer countStartTimeByRange(Date start, Date end) {
        return studentDAO.countStartTimeByRange(start, end);
    }

    @Override
    public Integer deleteByParam(Map<String, Object> params) {
        return studentDAO.deleteByParam(params);
    }

    @Override
    public List<Student> selectByExt2(String ext2) {
        return studentDAO.selectByExt2(ext2);
    }

    @Override
    public void updateSetExt2(Student student) {
        studentDAO.updateSetExt2(student);
    }

    @Override
    public ApiResponse<List<StudentResultVO>> list(StudentQueryVO studentQueryVO) {
        List<StudentResultVO> resultVOList = new ArrayList<>();
        Map<String, Object> param = MapUtils.buildQueryParam(studentQueryVO);
        Integer count = studentDAO.countStudent(param);
        List<Student> list = studentDAO.pageQuery(param);
        if (list != null && list.size() > 0) {
            for (Student student : list) {
                School school = schoolDAO.selectByPrimaryKey(student.getSchoolId());
                Class cls = classDAO.selectByPrimaryKey(student.getClassId());
                StudentResultVO resultVO = StudentResultVO.builder().build();
                BeanUtils.copyProperties(student, resultVO);
                resultVO.setParentMsg(student.getParentName() + "/" + student.getParentPhone());
                List<StudentLoginRel> relList = studentLoginRelDAO.selectByStudentId(student.getId());
                if (relList != null && relList.size() > 0) {
                    List<String> loginList = new ArrayList<>();
                    for (StudentLoginRel rel : relList) {
                        UserLogin login = userLoginDAO.selectByPrimaryKey(rel.getLoginId());
                        if (login != null) {
                            loginList.add(login.getAccount());
                        }
                    }
                    resultVO.setRelateAccount(loginList);
                }
                resultVO.setStatus(student.getStatus().intValue());
                resultVOList.add(resultVO);
                resultVO.setClassName(cls.getName());
                resultVO.setSchoolName(school.getName());
            }
        }
        return ApiResponse.success(resultVOList, count);
    }
}
